package com.example.onlyone;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.R.array;
import android.R.integer;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity implements OnGestureListener {

	private RelativeLayout mBtnRoot;
	private ArrayList<String> Array;
	final int RIGHT = 0;
	final int LEFT = 1;
	private int mRow = 6;
	private int mCol = 5;
	private static int s_PageNumber = 0;
	private GestureDetector gestureDetector;

	public void doResult(int action) {
		switch (action) {
		case RIGHT:
			Toast.makeText(this, "go right", Toast.LENGTH_SHORT).show();
			if ( s_PageNumber > 0 ) {
				s_PageNumber--;
			}
			break;
		case LEFT:
			Toast.makeText(this, "go left", Toast.LENGTH_SHORT).show();
			Toast.makeText(this, "" + getWeight(), Toast.LENGTH_SHORT).show();
			if ( s_PageNumber < 5 ) {
				s_PageNumber++;
			}
			break;
		}
	}
	
	public int getWeight() {
		return s_PageNumber*(mRow*mCol);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mBtnRoot = (RelativeLayout) findViewById(R.id.btn_lay);
		// AddMultiBtn(5,4);
		initArray(mRow * mCol);
		AddMultiBtn(mRow, mCol);
		gestureDetector = new GestureDetector(this,
				new GestureDetector.SimpleOnGestureListener() {
					@Override
					public boolean onFling(MotionEvent e1, MotionEvent e2,
							float velocityX, float velocityY) {
						final int Fling_Min_Distance = 50;
						float x = e2.getX() - e1.getX();
						float y = e2.getY() - e1.getY();
						// if (x > 0) {
						// doResult(RIGHT);
						// } else if (x < 0) {
						// doResult(LEFT);
						// }

						// return true;

						if (x > Fling_Min_Distance
								&& Math.abs(velocityX) > Math.abs(velocityY)) {
							doResult(RIGHT);
							return true;
						} else if (x < -Fling_Min_Distance
								&& Math.abs(velocityX) > Math.abs(velocityY)) {
							doResult(LEFT);
							return true;
						}

						return false;
					}
				});
		gestureDetector.setIsLongpressEnabled(true);
		// initArray(4*5);
		// addBtn();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return gestureDetector.onTouchEvent(event);
	}

	private void AddMultiBtn(int nRow, int col) {
		/*
		 * DisplayMetrics dm = new DisplayMetrics();
		 * getWindowManager().getDefaultDisplay().getMetrics(dm); int width =
		 * dm.widthPixels; int height = dm.heightPixels; int num = nRow*col;
		 * 
		 * Button Btn[] = new Button[num]; int j = -1; for (int i = 0; i < num;
		 * i++) { Btn[i]=new Button(this); Btn[i].setId(2000+i);
		 * Btn[i].setText("按钮"+i); RelativeLayout.LayoutParams btParams = new
		 * RelativeLayout.LayoutParams ((width-50)/4,height/nRow); //设置按钮的宽度和高度
		 * if (i%4 == 0) { j++; } btParams.leftMargin = 10+
		 * ((width-50)/4+10)*(i%4); //横坐标定位 btParams.topMargin = 20 + 55*j;
		 * //纵坐标定位 mBtnRoot.addView(Btn[i],btParams); //将按钮放入layout组件 }
		 */
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);

		int width = dm.widthPixels - 80;
		int height = dm.heightPixels - 200;
		// width = mBtnRoot.getWidth();
		// height = mBtnRoot.getHeight()-40;
		int nWidth = width / col;
		int nHeight = height / nRow;

		int chk_id = 1000;
		int num = nRow * col;
		RelativeLayout.LayoutParams relativeLayoutParams = null;
		Button Btn[] = new Button[num];
		int index = 0;
		for (int i = 0; i < nRow; i++) { // 控制行
			Btn[index] = new Button(this);
			Btn[index].setId(chk_id += 10);
			relativeLayoutParams = new RelativeLayout.LayoutParams(nWidth,
					nHeight);
			if (0 == i) { // 如果是第一行第一列，单独处理
				relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
				relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
			} else {
				relativeLayoutParams.addRule(RelativeLayout.ALIGN_LEFT,
						chk_id - 10);
				relativeLayoutParams.addRule(RelativeLayout.BELOW, chk_id - 10);
			}

			Btn[index].setText(Array.get(index));
			Btn[index].setOnTouchListener(new View.OnTouchListener() {
				public boolean onTouch(View v, MotionEvent event) {
					// TODO Auto-generated method stub
					return gestureDetector.onTouchEvent(event);
				}
			});
			Btn[index].setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(MainActivity.this,
							PlayActivity.class);
					Button btn = (Button) arg0;
					String strBtnName = btn.getText().toString();
					intent.putExtra("number", strBtnName);
					HashMap<Integer, Integer> nCellNumbers = new HashMap<Integer, Integer>();
					nCellNumbers.put(17, 1);
					nCellNumbers.put(18, 2);
					PlayActivity.mLevelMap = nCellNumbers;
					startActivity(intent);
				}
			});

			Btn[index].setLayoutParams(relativeLayoutParams);
			mBtnRoot.addView(Btn[index]);
			index++;
			// ******************
			for (int j = 1; j < col; j++) { // 控制列
				Btn[index] = new Button(this);
				Btn[index].setId(chk_id + j);
				Btn[index].setText(Array.get(index));
				relativeLayoutParams = new RelativeLayout.LayoutParams(nWidth,
						nWidth);
				relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, chk_id
						+ j - 1);
				relativeLayoutParams.addRule(RelativeLayout.ALIGN_TOP, chk_id
						+ j - 1);
				relativeLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM,
						chk_id + j - 1);
				Btn[index].setLayoutParams(relativeLayoutParams);
				Btn[index].setOnTouchListener(new View.OnTouchListener() {
					public boolean onTouch(View v, MotionEvent event) {
						// TODO Auto-generated method stub
						return gestureDetector.onTouchEvent(event);
					}
				});
				Btn[index].setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(MainActivity.this,
								PlayActivity.class);
						Button btn = (Button) arg0;
						String strBtnName = btn.getText().toString();
						intent.putExtra("number", strBtnName);
						HashMap<Integer, Integer> nCellNumbers = new HashMap<Integer, Integer>();
						nCellNumbers.put(17, 1);
						nCellNumbers.put(18, 2);
						PlayActivity.mLevelMap = nCellNumbers;
						startActivity(intent);
					}
				});
				mBtnRoot.addView(Btn[index]);
				index++;
			}
		}
	}

	private void initArray(int num) {
		Array = new ArrayList<String>();
		for (int i = 0; i < num; i++) {
			String strBtnName = new String("第") + (i + 1) + "关";
			Array.add(strBtnName);
		}
	}

	@SuppressLint("NewApi")
	private void addBtn() {
		int nCount = 0;
		String strNameHead = "BTN";
		for (int i = 0; i < 5; i++) {
			LinearLayout linearLayout = new LinearLayout(
					getApplicationContext());
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			lp.setMargins(0, 10, 0, 10);
			linearLayout.setLayoutParams(lp);
			linearLayout.setOrientation(LinearLayout.HORIZONTAL);
			Button btn1 = new Button(getApplicationContext());
			LayoutParams btnParams1 = new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			btn1.setLayoutParams(btnParams1);
			btn1.setText(strNameHead + nCount);
			btn1.setTextColor(Color.WHITE);
			// btn1.setBackgroundResource(R.drawable.button_selector);
			linearLayout.addView(btn1);

			nCount++;
			Button btn2 = new Button(getApplicationContext());
			LayoutParams btnParams2 = new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			btn2.setLayoutParams(btnParams2);
			btn2.setText(strNameHead + nCount);
			btn2.setTextColor(Color.WHITE);
			// btn2.setBackgroundResource(R.drawable.button_selector);
			linearLayout.addView(btn2);

			nCount++;
			Button btn3 = new Button(getApplicationContext());
			LayoutParams btnParams3 = new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			btn3.setLayoutParams(btnParams3);
			btn3.setText(strNameHead + nCount);
			btn3.setTextColor(Color.WHITE);
			// btn3.setBackgroundResource(R.drawable.button_selector);
			linearLayout.addView(btn3);

			nCount++;
			Button btn4 = new Button(getApplicationContext());
			LayoutParams btnParams4 = new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			btn4.setLayoutParams(btnParams4);
			btn4.setText(strNameHead + nCount);
			btn4.setTextColor(Color.WHITE);

			// btn4.setBackgroundResource(R.drawable.button_selector);
			linearLayout.addView(btn4);
			linearLayout.setWeightSum(1);
			mBtnRoot.addView(linearLayout);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onDown(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		float X = e2.getX() - e1.getX();
		float Y = e2.getY() - e1.getY();
		final int FLING_Min_DISTANCE = 50;

		if (X > FLING_Min_DISTANCE && Math.abs(velocityX) > Math.abs(velocityY)) {
			// toRight();
			// doResult(RIGHT);
		} else if (X < -FLING_Min_DISTANCE
				&& Math.abs(velocityX) > Math.abs(velocityY)) {
			// toLeft();
			// doResult(LEFT);
		}
		// else
		// if(Y>FLING_Min_DISTANCE&&Math.abs(velocityX)<Math.abs(velocityY)){
		// toDown();
		// }else
		// if(Y<-FLING_Min_DISTANCE&&Math.abs(velocityX)<Math.abs(velocityY)){
		// toUp();
		// }

		return false;
	}

	@Override
	public void onLongPress(MotionEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
			float arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}

}
